﻿
$(document).ready(function () {
    $('.ui-state-error').hide();
   // getFeed();

});

function getFeed() {

    $.post("/Feeds/GetFeedForUser", null,
            function (result) {

                var html = "";

                for (var i = 1; i < result.length; i++) {
                    
                    var data = result[i];
                    $.each(data, function (i, feed) {
                        html += feedHtmlBuilder(feed);
                    });
                }
                $("#user-feed").html(html);
                $("#wait-div").hide();
            }
         );

        }

        function feedHtmlBuilder(feed) {
            var html = "";
            html += "<div class=" + "row" + "><div class=" + "cols2>" + "<a href=" + "/" + "Project/View/" + feed.ProjectId + "><img src=" + feed.ProjectLogo +  "></a></div>";
            html += "<div  id=" + "feed-" + feed.FeedId + " " + " class=" + "cols2>" + "<p class=" + "p-status" + ">" + feed.StatusMessage + "</p> <p class=" + "p-updateinfo>" + "status by <b>" + feed.StatusBy + "</b><a id=" + feed.FeedId + " onclick=" + "comment(" + feed.FeedId + ");" + " href= " + "#>" + "<b  class=" + "b-comment>" + " comment </b></a> </p>";

            $.each(feed.Comments, function (j, cmt) {

                html += "<div id=" + "commentid-" + cmt.CommentId + " " + " class=" + "feed-comment><p class=" + "p-updateinfo> comment by " + cmt.CommentBy + "</p>" + cmt.CommentMessage + "</div>";
            });
            html += "</div></div>";

            return html;
        }

function comment(feedId) {

    var co = "<div class='add-comment'> <form id='comment-form'> <textarea  class='comment-txt-area'> </textarea> <input onclick='addComment(" + feedId + ");' class='comment-button' type='button' value='Add comment' /> </form> </div> ";
    var divId = "#feed-" + feedId;
    $('.add-comment').remove();
    $(divId).append(co);
}

function addComment(feedId) {

    var spot = $("#spot").val();

    $.post("/Feeds/CommentOnFeed",
                    {
                        feedId: feedId,
                        comment: $(".comment-txt-area").val()
                    },
                    function (result) {
                        if (result.error == true) {
                        }
                        else {
                            if (spot == "user") {
                                getFeed();
                            }
                            else {
                                getProjectFeed();
                            }

                        }
                    }
                );
                }

                function getProjectFeed() {
                    var projectId = $('#projectId').val();

                    $.getJSON("/" + "Feeds/GetProjectFeed",
                      {
                          projectId: projectId
                      },

                      function (result) {

                          var html = "";
                          
                          $.each(result, function (i, feed) {
                              html += feedHtmlBuilder(feed);
                          });

                          $('#feeds').html(html);
                      }
                  );
                }

                function updateStatus() {

                    var projectId = $('#projectId').val();
                    var msg = $('#status-txt').val();

                    $.post("/" + "Feeds/UpdateStatus",
                                        {
                                            projectId: projectId,
                                            sts: msg
                                        },

                                        function (result) {
                                            getProjectFeed();
                                        });

                    return false;

                }

